package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.Util.JwtUtil;
import org.example.anno.Log;
import org.example.pojo.Emp;
import org.example.pojo.Result;
import org.example.server.DeptServer;
import org.example.server.EmpServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class LoginController {

    @Autowired
    private EmpServer empServer;

    @Autowired
    private JwtUtil jwtUtil;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
        log.info("login");
        Emp e = empServer.login(emp);
        if (e != null){
            Map<String, Object> claims = new HashMap<>() ;
            claims.put("id",e.getId());
            claims.put("name",e.getName());
            claims.put("username",e.getUsername());
            String jwt = jwtUtil.createToken(claims);
            return Result.success(jwt);
        }
        return Result.error("用户名或密码错误");
    }
}
